package com.ptszyxx.popose.module.main.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.ShopTabEnum;
import com.ptszyxx.popose.databinding.FragmentMineShopTabBinding;
import com.ptszyxx.popose.module.main.mine.adapter.MineShopAdapter;
import com.ptszyxx.popose.module.main.mine.adapter.MineShopMyAdapter;
import com.ptszyxx.popose.module.main.mine.vm.MineShopTabVM;
import com.ysg.base.BaseFragment;
import com.ysg.enums.NoDiamondEnum;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.mine.ShopEntity;
import com.ysg.http.data.entity.mine.ShopMyEntity;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YRecyclerViewUtil;

import java.util.List;

public class MineShopTabFragment extends BaseFragment<FragmentMineShopTabBinding, MineShopTabVM> {
    private ShopTabEnum tab;
    private MineShopAdapter adapter;
    private MineShopMyAdapter myAdapter;
    public MineShopTabFragment(ShopTabEnum tab){
        this.tab = tab;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine_shop_tab;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MineShopTabVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MineShopTabVM.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        viewModel.tabEnum = tab;
    }

    @Override
    public void initData() {
        super.initData();
        if (viewModel.tabEnum == ShopTabEnum.avatar_my || viewModel.tabEnum == ShopTabEnum.mounts_my){
            viewModel.requestShopMy();
        }else {
            viewModel.requestShopList(true);
        }
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        binding.refreshLayout.setOnRefreshListener(o ->{
            viewModel.requestShopList(true);
        });
        binding.refreshLayout.setOnLoadMoreListener(o -> {
            viewModel.requestShopList(false);
        });

        if (viewModel.tabEnum == ShopTabEnum.avatar_my || viewModel.tabEnum == ShopTabEnum.mounts_my){
            binding.refreshLayout.setEnableRefresh(false);
            binding.refreshLayout.setEnableLoadMore(false);
            myAdapter = new MineShopMyAdapter(viewModel);
            YRecyclerViewUtil.initGrid(getContext(), binding.recyclerView, myAdapter, 3);
            binding.llBottom.setVisibility(View.GONE);
        }else{
            adapter = new MineShopAdapter(viewModel);
            YRecyclerViewUtil.initGrid(getContext(), binding.recyclerView, adapter, 3);
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, new Observer<List<ShopEntity>>() {
            @Override
            public void onChanged(List<ShopEntity> list) {
                setAdapterUI(list);
            }
        });
        viewModel.uc.onRefreshItemEvent.observe(this, o-> setAdapterItemUI());
        viewModel.uc.onBottomEvent.observe(this, o-> setBottomUI());
        viewModel.uc.onBuyEvent.observe(this, o-> showDialogBuy());
        viewModel.uc.onRechargeEvent.observe(this, o-> showDialogRecharge());

        viewModel.uc.onRefreshMyEvent.observe(this, new Observer<List<ShopMyEntity>>() {
            @Override
            public void onChanged(List<ShopMyEntity> list) {
                setAdapterMyUI(list);
            }
        });
    }

    private void setAdapterUI(List<ShopEntity> list) {
        binding.refreshLayout.finishRefresh();
        binding.refreshLayout.finishLoadMore();
        if (YCollectionUtil.isEmpty(list)) {
            binding.refreshLayout.finishLoadMoreWithNoMoreData();
            if (viewModel.isRefresh()){
                binding.stateLayout.showEmpty();
            }
            return;
        }
        binding.stateLayout.showContent();
        if (viewModel.isRefresh()){
            adapter.setList(list);
        }else{
            adapter.addData(list);
        }
    }

    private void setAdapterItemUI() {
        adapter.setCurrentPosition(viewModel.currentPosition);
        setBottomUI();
    }

    private void setBottomUI() {
        binding.tvDiamond.setText(viewModel.currentItem.getMoney() + "");
        String day = String.format(getResources().getString(R.string.mine_shop_day), viewModel.currentItem.getDay());
        binding.tvDay.setText(day);
    }

    private void setAdapterMyUI(List<ShopMyEntity> list) {
        myAdapter.setList(list);
    }

    /**
     * 购买
     */
    private void showDialogBuy() {
        String content = String.format(getResources().getString(R.string.dialog_buy_avatar_content), viewModel.currentItem.getName());
        YDialogUtil.getInstance().showConfirm(getContext(), content).setOnConfirmListener(()->{
            viewModel.requestShopBuy();
        });
    }
    /**
     * 钻石不足
     */
    private void showDialogRecharge() {
        YDialogUtil.getInstance().showNoDiamond(getContext(), NoDiamondEnum.gift, true);
    }
}
